using System;
using System.Collections.Generic;
using System.Data.Common;
using UnityEngine;
using UnityEngine.UI;

public class ChatListPanel : PanelCommon
{
    public Action<long> m_OnSelect;

    public Text candidateText;

    public TMPro.TMP_InputField inputField;

    public Action<string> m_OnSend;

    List<ChatCandidateItem> m_Items = new List<ChatCandidateItem>();
    List<ChatItem> m_CandidateChatItems = new List<ChatItem>();
    private List<ChatItem> m_PlayerChatItems = new List<ChatItem>();

    
    public Transform candidateItemsParent;
    public ChatCandidateItem itemPref;
    
    public Transform chatItemsParent;
    public ChatItem candidateChatItemPref;
    public ChatItem playerChatItemPref;

    public ScrollRect chatListScroll;
    private Vector3 m_NormalPos;

    ZUIAnimTools zTransformAnim;
    public ZUIAnimTools TransformAnim
    {
        get
        {
            if (zTransformAnim == null) 
                zTransformAnim = this.gameObject.AddComponent<ZUIAnimTools>();
            return zTransformAnim;
        }
    }
    public override void Awake()
    {
        base.Awake();

        itemPref.gameObject.SetActive(false);
        candidateChatItemPref.gameObject.SetActive(false);
        playerChatItemPref.gameObject.SetActive(false);
        
    }

    public override void Start()
    {
        base.Start();
        inputField.onSelect.AddListener(OnInputValueChanged);
        inputField.onEndEdit.AddListener(OnInputEndEvent);
        m_NormalPos = inputTrans.anchoredPosition;
    }
   
    public void Init(ChatListVCData data,Action<string> onSend)
    {
        m_OnSend = onSend;
        
        UnityFunctions.ClearList(m_Items);
        
        for (int i = 0; i < data.chats.Count; i++)
        {
            var newItem = Instantiate(itemPref, candidateItemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(new MiniUserInfo(MGame.instance.MUser.userInfo),data.chats[i], OnSelectItem);

            m_Items.Add(newItem);
        }

        var selectItem = m_Items.Find(e => e.Id == data.defaultChat);
        OnSelectItem(selectItem);

        var selectData = data.chats.Find(e => e.userId1 == data.defaultChat);

        if (selectData.dialogues != null)
        {
            UnityFunctions.ClearList(m_CandidateChatItems);
            UnityFunctions.ClearList(m_PlayerChatItems);
            
            for (int i = 0; i < selectData.dialogues.Length; i++)
            {
                ChatItem newItem;
                long candidateId = selectData.userId1;
                if (candidateId > 0)
                {
                    newItem = Instantiate(candidateChatItemPref, chatItemsParent);
                    m_CandidateChatItems.Add(newItem);
                }
                else
                {
                    newItem = Instantiate(playerChatItemPref, chatItemsParent);
                    m_PlayerChatItems.Add(newItem);
                }
                
//Debug.Log($"chat item:{selectData.candidateId} content:{selectData.dialogues[i].content}");
                newItem.gameObject.SetActive(true);
                newItem.Init(null, selectData.dialogues[i].message);
            }
        }

        //coinText.Init(MGame.Instance.user.coin,"","",0);
        this.candidateText.text = ConstantData.instance.GetCandidate(selectData.userId1).name;
        
        m_ToHide = false;
        
        this.chatListScroll.verticalNormalizedPosition = 0;
        Canvas.ForceUpdateCanvases();
        
        DoActionDelay.DelayAction(.4f, () =>
        {
            this.chatListScroll.verticalNormalizedPosition = 0;
            Canvas.ForceUpdateCanvases();

        });
    }
    private bool m_InTexting;
    public RectTransform inputTrans;
    private void OnInputValueChanged(string value)
    {
        if (!m_InTexting)
        {
            m_InTexting = true;
#if UNITY_EDITOR
            //return;
#endif
            
            var pos = m_NormalPos;
            pos.y = 960;
            //transform.localPosition = pos;
            TransformAnim.RectMove(inputTrans, pos, .2f, null,ZUIAnimTools.EaseType.OutSine);
        }
    }

    private void OnInputEndEvent(string arg0)
    {
        m_InTexting = false;
#if UNITY_EDITOR
        //return;
#endif
        //transform.localPosition = m_NormalPos;
        TransformAnim.StopAllAnim();
        TransformAnim.RectMove(inputTrans, m_NormalPos, .2f, null,ZUIAnimTools.EaseType.OutSine);
    }
    void OnSelectItem(ChatCandidateItem skinsItem)
    {
        if (!skinsItem)
            return;

        this.m_OnSelect?.Invoke(skinsItem.Id);
        
        /*foreach (var item in m_Items)
        {
            if(item == skinsItem)
                item.selectSign.gameObject.SetActive(true);
            else
                item.selectSign.gameObject.SetActive(false);
        }*/
    }

    public void OnClickSend()
    {
        string sendStr = this.inputField.text;
        m_OnSend?.Invoke(sendStr);
    }

    protected override void GoBackToView()
    {
        base.GoBackToView();
        
        GoBackToBattleUI(Vector2.zero);
    }

    bool m_ToHide;
    void GoBackToBattleUI(Vector2 pos)
    {
        if (m_ToHide)
            return;

        m_ToHide = true;

        //this.m_Controller.parentNav.LoadDisplay<MBattleUIViewController>();
        GetComponentInParent<MBattleSceneController>().ShowBattleUI();
    }

    public void Refresh(ChatListVCData getVcData)
    {
        Init(getVcData,m_OnSend);
    }
}